#include <STC15F2K60S2.H>
#include "Tim.h"
#include "Seg.h"
#include "Key.h"
#include "Ds1302.h"
#include "stdio.h"
#include "Onewire.h"
#include "Ptim.h"
#include "iic.h"
#include "LED.h"
//在温度之后读取会影响adda的读取会变成0，单总线会影响双总线，所以温度读读取要在双总线操作后面
//尽量都在if语句里面加括号
//只要数码管不涉及的变量做条件就可以直接实现，不考虑数码管影响
unsigned long ulms,ulKey_Time,ucStart;
unsigned char pucSeg_Buf[10],pucSeg_Code[8],ucSeg_Pos;//这里会影响数码管的显示，在几位的时候就要数组就要几位
unsigned int ucSeg_Dly;
unsigned char ucKey_Dly,ucKey_Old;
unsigned char ucState,ucState1;
unsigned char pucRtc[3]={13,3,50};
unsigned int uiTemp,uiTempMax,ucTemp=30;
unsigned int uccount;
unsigned int ucHumi,ucHumiMax;
unsigned char ucSec;
unsigned char ucpcf_U,ucpcf_Old,ucpcf_U1;
unsigned char ucSeg_Flag;
unsigned int unTemp,unHumi;
unsigned char ucLed,Seg_Flag;
unsigned char ucHumi_Flag;
void Seg_Proc(void);
void Key_Proc(void) ;
void Temp_Set(unsigned char ucKey_Down);
void Led_Set(void);
void main(){
	Cls_Peripheral() ;
	Timer1Init();
	Timer0Init();
	Set_RTC(pucRtc);
	while(1){
		Seg_Proc();
		Key_Proc() ;
		Led_Set();
}
}
void Time_1(void) interrupt 3 //定时器1中断函数
{ 
	 ulms++; 
	 if(!(ulms % 1000)) 
	 { 
		 uccount=(TH0<<8)+TL0;
		 TH0=0;
		 TL0=0;
		 if(uccount<=200){
			 ucHumi=100;
		 }
		 else if(uccount>=2000){
			 ucHumi=900;
		 }
		 else if(uccount>200&&uccount<2000){
			 ucHumi=(uccount-200)/180*80+100;
		 }
		 if(ucHumi>ucHumiMax)
			 ucHumiMax=ucHumi;
	} 
	 Led_Disp(ucLed);
	if(++Seg_Flag==100){
		Seg_Flag=0;
		if((unTemp/16)>ucTemp){
			if(!Seg_Flag){
				ucLed^=8;
			}
		}
		else{
			ucLed&=~8;
		}
	}		
	 if(++ucSeg_Dly==500) ucSeg_Dly=0;
	 if(++ucKey_Dly==20) ucKey_Dly=0;
	 Seg_Disp(pucSeg_Code, ucSeg_Pos);
	if(++ucSeg_Pos==8) ucSeg_Pos=0;
}
void Key_Proc(void) 
{ 
	 unsigned char ucKey_Val, ucKey_Down, ucKey_Up; 
	 if(ucKey_Dly) return; 
		ucKey_Dly = 1; 
	 ucKey_Val = Key_Read(); 
	 ucKey_Down = ucKey_Val & (ucKey_Old ^ ucKey_Val); 
	 ucKey_Up = ~ucKey_Val & (ucKey_Old ^ ucKey_Val); 
	 ucKey_Old = ucKey_Val; 
	if(!ucSeg_Flag){
			 switch(ucKey_Down)  { 
		 case 4:
			 if(++ucState==3) ucState=0;
			if(ucState==1) ucState1=0;
			 break;
		 case 5:
			 if(++ucState1==3) ucState1=0;
			break;
		 case 8:
			Temp_Set(ucKey_Down);
			 break;
		 case 9:
			 if(ucState1==2){
				ulKey_Time = ulms; 
			}
			 Temp_Set(ucKey_Down);
			 break;
	 }
	}

	 if(ucKey_Old==9 && (ulms - ulKey_Time > 2000))  { 
		ucSec=0;
	 } 
}
void Seg_Proc(void){
	if(ucSeg_Dly) return;
	ucSeg_Dly=1;
	ucpcf_U=PCF8591_Adc();//在温度之后读取会影响adda的读取会变成0，单总线会影响双总线，所以温度读读取要在双总线操作后面
	uiTemp=rd_temperature();
	if(uiTemp>uiTempMax&&(uiTemp/16)!=85)
		uiTempMax=uiTemp;
	
	if(!ucSeg_Flag){
			if(ucpcf_U==0){
				ucpcf_U1=0;
	}
	else{
		ucpcf_U1=1;
	}
	if(ucpcf_Old!=ucpcf_U1){
		ucpcf_Old=ucpcf_U1;
		if(ucpcf_U1==0){
			ucSeg_Flag=1;
			ucStart= ulms;
			if(ucHumi!=100&&ucHumi!=900){
				Read_RTC(pucRtc) ;
				if((uiTemp>unTemp||ucHumi>unHumi)&&ucSec>=2){
					ucLed|=32;//只要数码管不涉及的变量做条件就可以直接实现，不考虑数码管影响
				}
				else{
					ucLed&=~32;
				}
				unTemp=uiTemp;
				unHumi=ucHumi;
				ucSec++;
				ucHumi_Flag=1;
			}
			else{
				ucHumi_Flag=0;
			}
		}
	}
	}
	if(ulms - ucStart>=3000&&ucSeg_Flag==1){
		ucSeg_Flag=0;
	}
	if(ucSeg_Flag){
				if(!ucHumi_Flag){
					sprintf(pucSeg_Buf,"E  AA-AA");
				}
				else{
					sprintf(pucSeg_Buf,"E  %02d-%02d",unTemp/16,unHumi/10);
				}
			}
	else{
			switch(ucState){
		case 0:
			Read_RTC(pucRtc) ;
			sprintf(pucSeg_Buf,"%02d-%02d-%02d",(unsigned int)pucRtc[0],(unsigned int)pucRtc[1],(unsigned int)pucRtc[2]);
			break;
		case 1:
					switch(ucState1){
				case 0:
					if(ucSec==0){
						sprintf(pucSeg_Buf,"C        ");
					}
					else	{
							sprintf(pucSeg_Buf,"C %02d-%4.1f",uiTempMax/16,uiTemp/16.0);
					}
					break;
				case 1:
					if(ucSec==0){
						sprintf(pucSeg_Buf,"H        ");
					}
					else{
							sprintf(pucSeg_Buf,"H %02d-%4.1f",ucHumiMax/10,ucHumi/10.0);
					}
					break;
				case 2:
					if(ucSec==0){
							sprintf(pucSeg_Buf,"F%02d     ",(unsigned int)ucSec);
					}
					else{
							sprintf(pucSeg_Buf,"F%02d%02d-%02d",(unsigned int)ucSec,(unsigned int)pucRtc[0],(unsigned int)pucRtc[1]);
					}
					break;
			}
		
			break;
		case 2:
			sprintf(pucSeg_Buf,"P     %02d",ucTemp);
			break;
	}
	}

	Seg_Tran(pucSeg_Buf, pucSeg_Code);
}
void Temp_Set(unsigned char ucKey_Down){
	if(ucState==2){
		if(ucKey_Down==8){
			if(++ucTemp==100) ucTemp=0;
		}
		else if(ucKey_Down==9){
			if(ucTemp==0) ucTemp=100;
			ucTemp--;
		}
	}
}
void Led_Set(void){
	switch(ucState){
		case 0:
			ucLed&=~4;
			ucLed|=1;
			break;
		case 1:
			ucLed&=~1;
			ucLed|=2;
			break;
		case 2:
			ucLed&=~2;
			ucLed|=4;
			break;
	}
	if(ucHumi_Flag==0&&ucSec!=0){
		ucLed|=16;
	}
	else{
		ucLed&=~16;
	}
}